package app.species;

import java.awt.Color;

public class MutableColor extends Color implements Mutable{

	public final static MutableColor green = new MutableColor(Color.green.getRGB());
	public final static MutableColor red = new MutableColor(Color.red.getRGB());
	private static final long serialVersionUID = -8524876161504244978L;

	public MutableColor(int rgb){
		super(rgb);
	}
	public MutableColor(int r, int g, int b) {
		super(r, g, b);
		
	}

	@Override
	public Mutable mutate(final float probality,final float impact, SpeciesSim sim) {
//		System.out.println(probality + " ----- " + impact);
//		System.out.println();
		int r = getRed();
		int g = getGreen();
		int b = getBlue();
		// shift blue, red ,green -+ 1
		if (sim.random.nextBoolean()) {
			int n = 5;
			if (sim.random.nextBoolean(probality))
				n = (int) (255f*impact);
			r += (sim.random.nextBoolean()) ? -n : n;
			if (r > 255)
				r = 255;
			else if (0 > r)
				r = 0;
		}
		if (sim.random.nextBoolean()) {
			int n = 5;
			if (sim.random.nextBoolean(probality))
				n = (int) (255f*impact);
			g += (sim.random.nextBoolean()) ? -n : n;
			if (g > 255)
				g = 255;
			else if (0 > g)
				g = 0;
		}
		if (sim.random.nextBoolean()) {

			int n = 5;
			if (sim.random.nextBoolean(probality))
				n = (int) (255f*impact);
			b += (sim.random.nextBoolean()) ? -n : n;
			if (b > 255)
				b = 255;
			else if (0 > b)
				b = 0;
		}
		if ((r + g + b) / 3 > 200){
			r *= 0.9;
			g *= 0.9;
			b *= 0.9;
		}
		// System.out.println("old " + r + " " + g + " " + b);
		return new MutableColor(r, g, b);
		
	}

	public Mutable merge(Mutable parent1, Mutable parent2, SpeciesSim sim) {
		MutableColor p1 =(MutableColor) parent1;
		MutableColor p2 =(MutableColor) parent2; 
		if (!parent1.equals(parent1)) {
			int r = (p1.getRed() + p2.getRed()) / 2;
			int g = (p1.getGreen() + p2.getGreen()) / 2;
			int b = (p1.getBlue() + p2.getBlue()) / 2;
			return  new MutableColor(r, g, b);
		}
		else{
			return new MutableColor(p1.getRGB());
		}
	}
	
	

}
